package com.atguigu.tingshu.user.client;

import com.atguigu.tingshu.common.cache.GuiguCache;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.*;
import com.atguigu.tingshu.user.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping(value = "/client/User/UserInfo")
public class UserClientController {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserPaidAlbumMapper paidAlbumMapper;

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;


    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;


    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @GuiguCache(prefix = "getUserInfo")
    @GetMapping("/getUserInfo/{userId}")
    UserInfo getAlbumInfoStat(@PathVariable(value = "userId") Long userId) {
        return userInfoMapper.selectById(userId);
    }

    /**
     * 购买专辑
     * @param userId
     * @param albumId
     * @return
     */
    @GetMapping("BuyAlbum/{userId}/{albumId}")
    UserPaidAlbum BuyAlbum(@PathVariable(value = "userId") Long userId,@PathVariable(value = "albumId") Long albumId){
        return paidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>()
                .eq(UserPaidAlbum::getUserId,userId)
                .eq(UserPaidAlbum::getAlbumId,albumId));
    }

    /**
     * 购买声音
     * @param userId
     * @param albumId
     * @return
     */
    @GetMapping("buytrack/{userId}/{albumId}")
    List<Long> buytrack(@PathVariable(value = "userId")Long userId,@PathVariable(value = "albumId") Long albumId){
        return userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>()
                        .eq(UserPaidTrack::getUserId,userId)//当前用户
                        .eq(UserPaidTrack::getAlbumId,albumId)//当前专辑
                        .select(UserPaidTrack::getTrackId)).stream()//所有购买记录，获取trackid字段即可
                        .map(UserPaidTrack::getTrackId).collect(Collectors.toList());
    }
    /**
     * 获取vip
     * @param vipId
     * @return
     */
    @GetMapping("/getVipService/{vipId}")
    VipServiceConfig getVipService(@PathVariable(value = "vipId") Long vipId){
        return vipServiceConfigMapper.selectById(vipId);

    }


    @GetMapping("/isOwn/{userId}{albumId}/{trackId}")
    Boolean isOwn (@PathVariable(value = "userId") Long userId,@PathVariable(value = "albumId") Long albumId,@PathVariable(value = "trackId")Long trackId){
        UserPaidTrack userPaidTrack = userPaidTrackMapper.selectOne(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getUserId, userId).eq(UserPaidTrack::getAlbumId, albumId).eq(UserPaidTrack::getTrackId, trackId));
        UserPaidAlbum userPaidAlbum = paidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userId).eq(UserPaidAlbum::getAlbumId, albumId));
        if (userPaidAlbum != null || userPaidTrack != null){
            return true;
        }
        return false;
    }

}